//
//  CHNSAttributedString.swift
//  CHSimpleUI
//
//  Created by CY on 2024/11/8.
//

import UIKit

extension  NSMutableAttributedString {

    /**
          两种颜色字体处理
     @param allText 全文
     @partText partText 需要处理的文字
      @param color 需要处理字体颜色
     @param font 需要处理字体
      @param oriFont 全文字体
     @param oriColor 全文字体颜色
     */
    public func partText(allText:String,partText:String,color:UIColor,font:UIFont,oriFont:UIFont? = nil,oriColor:UIColor? = nil) -> NSMutableAttributedString{
        let oriFontM = oriFont
        let oriColorM = oriColor
    
        let paragraphStyle:NSMutableParagraphStyle = NSMutableParagraphStyle.init()

        let allStrig:NSString = allText as NSString
        let mutAttString:NSMutableAttributedString = NSMutableAttributedString.init(string:allText)
 
        mutAttString.addAttributes([NSAttributedString.Key.foregroundColor:oriColorM,NSAttributedString.Key.paragraphStyle:paragraphStyle,NSAttributedString.Key.font:oriFontM], range: NSMakeRange(0, mutAttString.length))

        let clickT:NSString = partText as NSString
        
        let range:NSRange? = allStrig.range(of: clickT as String)
        
        mutAttString.addAttributes([ NSAttributedString.Key.foregroundColor:color,
                                     NSAttributedString.Key.font:font], range: range!)
        
        return mutAttString
     
    }
}
